local skel = fk.CreateSkill {
  name = "lb__muyu",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb__muyu"] = "暮雨",
  [":lb__muyu"] = "当你每回合使用首张【杀】时，你可以将之改为雷【杀】或冰【杀】；结束阶段，若本回合进入弃牌堆的♠牌总点数大于8，你可令一名角色进行【闪电】判定，且你可以随机将其中一张♠牌作为判定牌。",

  ["#lb__muyu_trigger"] = "暮雨",
  ["#lb__muyu_retrial"] = "暮雨",
  ["#lb__muyu"] = "暮雨：你可以将此【杀】改为雷【杀】或冰【杀】",
  ["#lb__muyu-ask"] = "暮雨：你可令一名角色进行【闪电】判定",
  ["#lb__muyu-retrial"] = "暮雨：你可以令其中随机一张牌作为 %dest的【闪电】判定牌",

  ["$lb__muyu1"] = "此生如朝露，身名俱灭。",
  ["$lb__muyu2"] = "忘川无波澜，引渡徘徊。",
  ["$lb__muyu3"] = "我为逝者哀哭……",
  ["$lb__muyu4"] = "暮雨，终将落下。",
}


skel:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "offensive",
  audio_index = { 1, 2 },
  can_trigger = function(self, _, target, player, data)
    if player:hasSkill(skel.name) and target == player and data.card.trueName == "slash" then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        return e.data.from == player and e.data.card.trueName == "slash"
      end, Player.HistoryTurn)
      return #use_events == 1 and use_events[1].data == data
    end
  end,
  on_cost = function(self, event, target, player, data)
    local all = { "thunder__slash", "ice__slash", "Cancel" }
    local ask = table.clone(all)
    for _, s in ipairs({ "thunder__slash", "ice__slash" }) do
      if data.card.name == s then
        table.removeOne(ask, s)
      end
    end
    local choice = player.room:askToChoice(player, {
      choices = ask,
      all_choices = all,
      skill_name = skel.name,
      prompt = "#lb__muyu",
    })
    event:setCostData(self, { name = choice })
    return choice ~= "Cancel"
  end,
  on_use = function(self, event, target, player, data)
    local name = event:getCostData(self).name
    data:changeCard(name, data.card.suit, data.card.number, skel.name)
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  audio_index = { 1, 2 },
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(skel.name) and target == player and player.phase == Player.Finish then
      local n = 0
      for _, id in ipairs(DIY.getCenterCards(room)) do
        if Fk:getCardById(id).suit == Card.Spade then
          n = n + Fk:getCardById(id).number
          if n > 8 then return true end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ps = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      prompt = "#lb__muyu-ask",
    })
    if #ps == 1 then
      event:setCostData(self, { tos = ps })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local p = event:getCostData(self).tos[1]
    local pattern = ".|2~9|spade"
    local judge = {
      who = p,
      reason = "lightning",
      pattern = pattern,
      extra_data = { lb__muyu = player.id },
    }
    room:judge(judge)
    if judge:matchPattern() then
      room:damage {
        to = p,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = "lb__muyu",
      }
    end
  end,
})


skel:addEffect(fk.AskForRetrial, {
  is_delay_effect = true,
  audio_index = { 3, 4 },
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.reason == "lightning" and data.extra_data and
    data.extra_data.lb__muyu == player.id
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(DIY.getCenterCards(room), function(id)
      return Fk:getCardById(id).suit == Card.Spade
    end)
    if #cards > 0 then
      if room:askToViewCardsAndChoice(player, {
            cards = cards,
            skill_name = skel.name,
            choices = { "OK", "Cancel" },
            prompt = "#lb__muyu-retrial::" .. data.who.id,
          }) == "OK" then
        event:setCostData(self, { card = table.random(cards) })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:changeJudge {
      card = Fk:getCardById(event:getCostData(self).card),
      player = player,
      data = data,
      skillName = "lb__muyu",
    }
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel
